package org.gwu.seuss.server.command;

import org.gwu.seuss.communication.domain.UserTO;
import org.gwu.seuss.communication.request.ClientRequest;
import org.gwu.seuss.communication.request.ServerResponse;
import org.springframework.util.Assert;

/**
 * 
 * This command is for basic users to update their password.
 * 
 */
public class UpdatePasswordCommand extends UserCommandBase implements Command {

	public UpdatePasswordCommand(ClientRequest req) {
		super(req);
	}

	@Override
	public ServerResponse execute() throws Exception {
		String username = req.getUsername();
		String oldPassword = req.getPassword();
		UserTO newUser = req.getUser();

		Assert.hasText(username, "Username is not provided.");
		Assert.hasText(oldPassword, "Old password is not provided.");
		Assert.notNull(newUser, "New user is not provided");

		String newPassword = newUser.getPassword();

		Assert.hasText(newPassword, "New password is not provided.");

		log.info("User " + username + "requested to update password.");

		int result = userService.resetPassword(username, newPassword,
				oldPassword);

		return getServerResponse(result, "User " + username
				+ " password updated.");
	}
}